﻿<script setup lang="ts" name="DepartmentManagement">
import { getDepartmentList } from '@/api/department'
import DepartmentAddEdit from './components/departmentAddEdit.vue'
import useDictionaryEntry from '@/hooks/useDictionaryEntry'
import { transformI18n } from '@/plugins/i18n'
import { cloneDeep } from 'lodash-es'
const { getDictionaryEntryData } = useDictionaryEntry()
const statusList = ref([])
const pageData:any = ref({
  expandList: [],
  listData: {
    data: []
  },
  searchData: {
    title: '',
    status: 1
  }
})
// 重置页面数据
const resetPageData = () => {
  pageData.value.listData.data = []
  pageData.value.expandList = []
}
// 获取当前查询条件下的数据
const getInitData = () => {
  resetPageData()
  const result: any = {}
  result.name = pageData.value.searchData.title
  getDepartmentList(result, { config: { showLoading: false, mockEnable: true } }).then((res:any) => {
    console.log(res)
    if (res.data && res.data.length > 0) {
      setTimeout(() => {
        pageData.value.listData.data = dataToTree(res.data)
        pageData.value.listData.data.forEach((item) => {
          pageData.value.expandList.push(item.id + '')
        })
      }, 1000)
    }
  })
}
const dataToTree = (data) => {
  const dataArr = cloneDeep(data)
  const parents = dataArr.filter(function (item: any) {
    return item.pid === 0
  })
  const children = dataArr.filter(function (item: any) {
    return item.pid !== 0
  })
  // 递归处理动态路由层级
  convert(parents, children)
  return parents
}
const convert = (parents: any, children: any) => {
  parents.forEach(function (item: any) {
    item.value = item.id
    item.label = item.name
    item.children = []
    children.forEach(function (current: any, index: any) {
      if (current.pid === item.id) {
        const temp = children.filter(function (v: any, idx: any) {
          return idx !== index
        }) // 删除已匹配项，这里未使用JSON.parse(JSON.stringify(children))是因为路由的component已经是函数形式，再格式化后，模块导入功能丢失，会导致找不到模块
        item.children.push(current)
        convert([current], temp) // 递归
      }
    })
  })
}
// 查询条件重置
const resetSearch = () => {
  pageData.value.searchData.title = ''
  pageData.value.searchData.status = 1
  getInitData()
}

const departmentDialog = ref(null)
// 新增事件
const add = () => {
  departmentDialog.value.showDialog()
}
// 编辑事件
const edit = (item) => {
  departmentDialog.value.showDialog(item)
}

const deleteRow = (v) => {
  const msg = transformI18n('page.确定删除吗？', true)
  ElMessageBox.alert(msg, transformI18n('moduleTitle.警告', true), {
    showCancelButton: true,
    confirmButtonText: transformI18n('buttons.buttonConfirm', true),
    cancelButtonText: transformI18n('buttons.buttonCancel', true),
    type: 'warning',
    callback: (action) => {
      if (action === 'confirm') {
        ElMessage({
          type: 'info',
          message: `操作: 点击了确认`
        })
      } else {
        ElMessage({
          type: 'info',
          message: `操作: 点击了取消`
        })
      }
    }
  })
}

const getStatusList = async () => {
  statusList.value = await getDictionaryEntryData({
    label: '',
    status: 2,
    type: 'enable_number'
  })
}
getStatusList()
onMounted(() => {
  getInitData()
})
</script>

<template>
  <page-wrap direction="column">
    <el-form :inline="true">
      <el-form-item :label="$t('page.部门名称')">
        <el-input v-model="pageData.searchData.title" :placeholder="$t('placeholders.请输入') + $t('page.部门名称')" />
      </el-form-item>
      <el-form-item :label="$t('page.状态')">
        <el-radio-group class="radio-button" v-model="pageData.searchData.status">
          <el-radio-button v-for="(item, index) in statusList" :key="index" :label="item.value">{{ $t('page.' + item.label) }}</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="getInitData">{{ $t('buttons.buttonSearch') }}</el-button>
        <el-button icon="Refresh" @click="resetSearch">{{ $t('buttons.buttonReset') }}</el-button>
        <el-button type="success" icon="Plus" @click="add()">{{ $t('buttons.buttonCreate') }}{{ $t('page.部门') }}</el-button>
      </el-form-item>
    </el-form>
    <div class="flex-1 table-layout">
      <div class="table-wrap">
        <el-table :data="pageData.listData.data" border row-key="id" :expand-row-keys="pageData.expandList">
          <el-table-column prop="name" :label="$t('page.部门名称')" />
          <el-table-column prop="status" :label="$t('page.状态')" width="90" align="center">
            <template #default="scope">
              <el-tag :type="scope.row.status ? 'success' : 'danger'">{{ scope.row.status ? $t('page.启用') : $t('page.停用') }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="sort" :label="$t('page.排序')" width="60" align="center" />
          <el-table-column prop="remark" :label="$t('page.备注')" />
          <el-table-column prop="operate" :label="$t('page.操作')" width="140" align="center">
            <template #default="scope">
              <el-button type="primary" icon="Edit" @click="edit(scope.row)"></el-button>
              <el-button type="danger" icon="Delete" @click="deleteRow(scope.row)"></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <DepartmentAddEdit ref="departmentDialog" :listData="pageData.listData.data"></DepartmentAddEdit>
  </page-wrap>
</template>

<style scoped lang="scss">
.table-layout {
  position: relative;
  margin: var(--el-space) 0 0;

  .table-wrap {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;

    .el-table {
      width: 100%;
      height: 100%;
    }
  }
}

.el-pagination {
  align-items: center;
  justify-content: flex-end;
}
</style>
